#!/bin/bash
#确认是否已经安装docker
echo "********************************************"
echo "此脚本用于卸载SoData安装时,安装的Docker服务"
echo "********************************************"


function print(){
	echo -e "$2> $1 $3"
}
function print_error(){
	print "$1" '\e[31m' '\e[0m'
}
function print_success(){
	print "$1" '\e[34m' '\e[0m'
}


de=`docker --version 2>/dev/null`
if [ "$?" -ne "0" ];then
	print_error "docker 命令未找到,脚本退出..."
	exit 0;
fi

ccount=`docker ps -a|grep -v "CONTAINER ID" |awk '{print $1}'`
if [ -n "$ccount" ];then
	print_error "卸载失败，存在未清理的容器！卸载命令:\n\t docker rm 容器ID1 容器ID2 ... \n\nCONTAINER ID\n$ccount"
	exit 1
fi
print_success "检测残留容器成功！"
icount=`docker image ls |grep -v "IMAGE ID" |awk '{print $3}'`
if [ -n "$icount" ];then
        print_error "卸载失败，存在未清理的镜相 卸载命令:\n\t docker rmi 镜相ID1 镜相ID2 ... \n\nIMAGE ID\n$icount"
        exit 1
fi
print_success "检测残留镜像成功！"
if [ "$?" -ne "0" ];then
	print_error "Docker卸载失败!脚本退出..."
        exit 1;
fi
#停止docker
systemctl stop docker
if [ "$?" -ne "0" ];then
	print_error "Docker停止失败!脚本退出..."
        exit 1;
fi
print_success "停止Dodker服务成功！"
print "清理Dodker安装文件..."
rm -f /lib/systemd/system/docker.service
#rm -rf docker
DOCKER_CONFIG=${DOCKER_CONFIG:-$HOME/.docker}
rm -f $DOCKER_CONFIG/cli-plugins/docker-compose
cd /usr/bin && rm -rf containerd  containerd-shim  containerd-shim-runc-v2  ctr  docker* dockerd  docker-init  docker-proxy  runc  /var/run/docker /var/run/docker.sock /var/lib/docker
if [ "$?" -ne "0" ];then
        print_error "Docker卸载失败!脚本退出..."
	exit 1;
fi
print_success "清理Dodker安装文件成功!"
#cd -
print_success "============Docker卸载成功!!!============"
